package com.flower.wing.oracle;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class OracleConnector {
	
	static String loginSQL = null;
	static Properties info;
	
	static Connection conn = null;
	static Statement stmt = null;
	static ResultSet rs = null;
	
	int x, y;
	static File file = null;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		try {

			// 1. 드라이버 로딩  C:\Program Files\Java\jre6\lib\ext
			Class.forName("oracle.jdbc.driver.OracleDriver");

			// 드라이버 경로-(패키지명) .(클래스명)
			System.out.println("드라이버 로딩 성공!!");

			// 2. DB와 연결
			file = new File("./info/dbInfo.properties");
			info = new Properties();
			info.load(new FileInputStream(file));
			System.out.println("DB와의 연결을 시도합니다.");
			Class.forName(info.getProperty("DBDRIVER"));
			conn = DriverManager.getConnection(info.getProperty("DBURL"),
					info.getProperty("DBID"), info.getProperty("DBPWD"));
//			Connection con = DriverManager.getConnection(url, user, pwd);
			System.out.println("DB와의 연결이 성공하였습니다.");
			
//			=============test============
			String id = "flower";
			String userID = null, userPWD = null;
			
			loginSQL = "SELECT USER_INFO.USER_ID, USER_INFO.USER_PW FROM USER_INFO WHERE USER_INFO.USER_ID = '" + id + "'";
			
			System.out.println(loginSQL);
			
			stmt = conn.createStatement();
			rs = stmt.executeQuery(loginSQL);
			
			while (rs.next()){
				userID = rs.getString(1);
				userPWD = rs.getString(2);
			}
			System.out.println(userID + " <<-- realID");
			System.out.println(userPWD + " <<-- realPWD");
			System.out.println(id + " <<-- inputID");
//			=============test============
			
			System.out.println("DB와 연결 끊기를 시도합니다.");
			conn.close(); // DB와 연결된 자원 반납 (중요)
			System.out.println("DB와의 연결 끊기를 성공하였습니다.");

			// 예외 처리
		} catch (ClassNotFoundException e) {
			System.out.println("드라이버 로딩 실패!");
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
